﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayController : MonoBehaviour
{
    private Rigidbody _rigidbody;
    private Animator _anim;
    private AudioSource _audioSource;

    public ParticleSystem dirtParticle, explotionParticle;
    public AudioClip jumpClip, crashClip;

    public float gravityModifier;
    public float jumpForce;

    public bool isGround = false;
    public bool isGameOver = false;
    // Start is called before the first frame update
    void Start()
    {
        _rigidbody = GetComponent<Rigidbody>();
        _anim = GetComponent<Animator>();
        _audioSource = GetComponent<AudioSource>();

        Physics.gravity *= gravityModifier;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGround) 
        {
            _rigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isGround = false;
            _anim.SetTrigger("Jump_trig");
            
            dirtParticle.Stop();

            _audioSource.PlayOneShot(jumpClip, 1.0f);
        }
        
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag=="Obstacle")
        {
            isGameOver = true;
            Debug.Log("GameOver");
            _anim.SetBool("Death_b", true);
            _anim.SetInteger("DeathType_int", 1);
            explotionParticle.Play();

            _audioSource.PlayOneShot(crashClip, 1.0f);
        }
        if (collision.gameObject.tag == "Ground")
        {
            isGround = true;
            dirtParticle.Play();
        }

       
    }
}
